package com.keyue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keyue.common.constant.Constant;
import com.keyue.common.constant.ServerResult;
import com.keyue.common.exception.BusinessException;
import com.keyue.common.utils.PageUtils;
import com.keyue.common.utils.TransactionHelper;
import com.keyue.domain.dto.*;
import com.keyue.domain.vo.*;
import com.keyue.mapper.EquipmentInfoMapper;
import com.keyue.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 设备信息(EquipmentInfo)表服务实现类
 *
 * @author makejava
 * @since 2025-07-04 15:20:04
 */
@Slf4j
@Service
public class EquipmentInfoServiceImpl extends ServiceImpl<EquipmentInfoMapper, EquipmentInfoDO> implements EquipmentInfoService {

    @Resource
    private EquipmentParamInfoService paramInfoService;

    @Resource
    private EquipmentIntroductionInfoService introductionInfoService;

    @Resource
    private AddressInfoService addressInfoService;

    @Resource
    private AddressBindingService addressBindingService;

    @Resource
    private TransactionHelper transactionHelper;

    /**
     * 通过ID查询单条数据
     * <p>
     * queryVO 主键
     *
     * @return 实例对象
     */
    @Override
    public ServerResult queryById(EquipmentInfoQueryVO queryVO) {
        if (Objects.isNull(queryVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        EquipmentInfoDO equipmentInfoDO = getById(queryVO.getId());
        if (Objects.isNull(equipmentInfoDO)) {
            return ServerResult.success(null);
        }
        EquipmentInfoVO equipmentInfoVO = BeanUtil.copyProperties(equipmentInfoDO, EquipmentInfoVO.class);
        List<AddressBindingDO> addressBindingDOS = addressBindingService.list(new LambdaQueryWrapper<AddressBindingDO>().eq(AddressBindingDO::getEquipmentId, equipmentInfoDO.getId()));
        if (CollectionUtils.isNotEmpty(addressBindingDOS)) {
            equipmentInfoVO.setAddressIds(addressBindingDOS.stream().map(AddressBindingDO::getAddressId).collect(Collectors.toList()));
        }
        if (StringUtils.isNotBlank(equipmentInfoDO.getImgItems())) {
            equipmentInfoVO.setImageItemVOList(JSONArray.parseArray(equipmentInfoDO.getImgItems(), ImageItemVO.class));
        }
        if (StringUtils.isNotBlank(equipmentInfoDO.getMasterImgItems())) {
            equipmentInfoVO.setMasterItemVOList(JSONArray.parseArray(equipmentInfoDO.getMasterImgItems(), ImageItemVO.class));
        }
        if (StringUtils.isNotBlank(equipmentInfoDO.getParamImgItems())) {
            equipmentInfoVO.setParamItemVOList(JSONArray.parseArray(equipmentInfoDO.getParamImgItems(), ImageItemVO.class));
        }
        if (StringUtils.isNotBlank(equipmentInfoDO.getScopeImgItems())) {
            equipmentInfoVO.setScopeItemVOList(JSONArray.parseArray(equipmentInfoDO.getScopeImgItems(), ImageItemVO.class));
        }
        if (StringUtils.isNotBlank(equipmentInfoDO.getIntroduceImgItems())) {
            equipmentInfoVO.setIntroduceItemVOList(JSONArray.parseArray(equipmentInfoDO.getIntroduceImgItems(), ImageItemVO.class));
        }
        EquipmentParamInfoDO paramInfoDO = paramInfoService.getOne(new LambdaQueryWrapper<EquipmentParamInfoDO>()
                .eq(EquipmentParamInfoDO::getEnableFlag, Constant.ENABLE_TEN).eq(EquipmentParamInfoDO::getEquipmentId, queryVO.getId()));
        if (Objects.nonNull(paramInfoDO)) {
            EquipmentParamInfoVO paramInfoVO = BeanUtil.copyProperties(paramInfoDO, EquipmentParamInfoVO.class);
            equipmentInfoVO.setParamInfoVO(paramInfoVO);
        }

        List<EquipmentIntroductionInfoDO> introductionInfoDOS = introductionInfoService.list((new LambdaQueryWrapper<EquipmentIntroductionInfoDO>()
                .eq(EquipmentIntroductionInfoDO::getEnableFlag, Constant.ENABLE_TEN).eq(EquipmentIntroductionInfoDO::getEquipmentId, queryVO.getId())));
        if (CollectionUtils.isNotEmpty(introductionInfoDOS)) {
            List<EquipmentIntroductionInfoVO> introductionInfoVOS = introductionInfoDOS.stream().map(introductionInfoDO -> {
                EquipmentIntroductionInfoVO introductionInfoVO = BeanUtil.copyProperties(introductionInfoDO, EquipmentIntroductionInfoVO.class);
                if (StringUtils.isNotBlank(introductionInfoDO.getLineTextJson())) {
                    introductionInfoVO.setLineTextItemVOS(JSONArray.parseArray(introductionInfoDO.getLineTextJson(), LineTextItemVO.class));
                }
                if (StringUtils.isNotBlank(introductionInfoDO.getImageItemJson())) {
                    introductionInfoVO.setImageTextItemVOS(JSONArray.parseArray(introductionInfoDO.getImageItemJson(), ImageTextItemVO.class));
                }
                return introductionInfoVO;
            }).collect(Collectors.toList());
            equipmentInfoVO.setIntroductionInfoVOS(introductionInfoVOS);
        }

        return ServerResult.success(equipmentInfoVO);
    }

    /**
     * 分页查询
     *
     * @param queryVO 筛选条件
     * @return 查询结果
     */
    @Override
    public ServerResult listByPage(EquipmentInfoQueryVO queryVO) {

        LambdaQueryWrapper<EquipmentInfoDO> queryWrapper = buildQueryWrapper(queryVO);
        Page page = new Page<>(queryVO.getPage(), queryVO.getPageSize());
        IPage<EquipmentInfoDO> iPage = page(page, queryWrapper);
        queryVO.setTotal(Long.valueOf(iPage.getTotal()).intValue());
        List<EquipmentInfoDO> equipmentInfoDOS = iPage.getRecords();
        if (CollectionUtils.isEmpty(equipmentInfoDOS)) {
            return ServerResult.success(PageUtils.getUnifiedEmptyPage(queryVO));
        }
        Set<Long> equipmentIds = equipmentInfoDOS.stream().map(EquipmentInfoDO::getId).collect(Collectors.toSet());
        List<AddressBindingDO> addressBindingDOS = addressBindingService.list(new LambdaQueryWrapper<AddressBindingDO>().in(AddressBindingDO::getEquipmentId, equipmentIds));
        Map<Long, List<AddressBindingDO>> addressIdMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(addressBindingDOS)) {
            addressIdMap = addressBindingDOS.stream().collect(Collectors.groupingBy(AddressBindingDO::getEquipmentId));
        }
        List<EquipmentParamInfoDO> paramInfoDOS = paramInfoService.list(new LambdaQueryWrapper<EquipmentParamInfoDO>()
                .eq(EquipmentParamInfoDO::getEnableFlag, Constant.ENABLE_TEN)
                .in(EquipmentParamInfoDO::getEquipmentId, equipmentIds));
        List<EquipmentIntroductionInfoDO> introductionInfoDOS = introductionInfoService.list(new LambdaQueryWrapper<EquipmentIntroductionInfoDO>()
                .eq(EquipmentIntroductionInfoDO::getEnableFlag, Constant.ENABLE_TEN)
                .in(EquipmentIntroductionInfoDO::getEquipmentId, equipmentIds));
        Map<Long, EquipmentParamInfoDO> paramInfoDOMap = CollectionUtils.isEmpty(paramInfoDOS) ? new HashMap<>() : paramInfoDOS.stream().collect(Collectors.toMap(EquipmentParamInfoDO::getEquipmentId, Function.identity()));
        Map<Long, List<EquipmentIntroductionInfoDO>> introductionInfoMap = CollectionUtils.isEmpty(introductionInfoDOS) ? new HashMap<>() : introductionInfoDOS.stream().collect(Collectors.groupingBy(EquipmentIntroductionInfoDO::getEquipmentId));
        Map<Long, List<AddressBindingDO>> finalAddressIdMap = addressIdMap;
        List<EquipmentInfoVO> equipmentInfoVOS = equipmentInfoDOS.stream().map(x -> {
            EquipmentInfoVO equipmentInfoVO = new EquipmentInfoVO();
            BeanUtil.copyProperties(x, equipmentInfoVO);
            if (finalAddressIdMap.containsKey(x.getId())) {
                List<AddressBindingDO> list = finalAddressIdMap.get(x.getId());
                equipmentInfoVO.setAddressIds(list.stream().map(AddressBindingDO::getAddressId).collect(Collectors.toList()));
            }
            if (StringUtils.isNotBlank(x.getImgItems())) {
                equipmentInfoVO.setImageItemVOList(JSONArray.parseArray(x.getImgItems(), ImageItemVO.class));
            }
            if (StringUtils.isNotBlank(x.getMasterImgItems())) {
                equipmentInfoVO.setMasterItemVOList(JSONArray.parseArray(x.getMasterImgItems(), ImageItemVO.class));
            }
            if (StringUtils.isNotBlank(x.getParamImgItems())) {
                equipmentInfoVO.setParamItemVOList(JSONArray.parseArray(x.getParamImgItems(), ImageItemVO.class));
            }
            if (StringUtils.isNotBlank(x.getScopeImgItems())) {
                equipmentInfoVO.setScopeItemVOList(JSONArray.parseArray(x.getScopeImgItems(), ImageItemVO.class));
            }
            if (StringUtils.isNotBlank(x.getIntroduceImgItems())) {
                equipmentInfoVO.setIntroduceItemVOList(JSONArray.parseArray(x.getIntroduceImgItems(), ImageItemVO.class));
            }
            if (paramInfoDOMap.containsKey(x.getId())) {
                EquipmentParamInfoDO equipmentParamInfoDO = paramInfoDOMap.get(x.getId());
                EquipmentParamInfoVO paramInfoVO = new EquipmentParamInfoVO();
                BeanUtil.copyProperties(equipmentParamInfoDO, paramInfoVO);
                equipmentInfoVO.setParamInfoVO(paramInfoVO);
            }
            if (introductionInfoMap.containsKey(x.getId())) {
                List<EquipmentIntroductionInfoDO> introductionInfoDOList = introductionInfoMap.get(x.getId());
                if (CollectionUtils.isNotEmpty(introductionInfoDOList)) {
                    List<EquipmentIntroductionInfoVO> introductionInfoVOS = introductionInfoDOList.stream().map(infoDO -> {
                        EquipmentIntroductionInfoVO introductionInfoVO = new EquipmentIntroductionInfoVO();
                        BeanUtil.copyProperties(infoDO, introductionInfoVO);
                        if (StringUtils.isNotBlank(infoDO.getLineTextJson())) {
                            introductionInfoVO.setLineTextItemVOS(JSONArray.parseArray(infoDO.getLineTextJson(), LineTextItemVO.class));
                        }
                        if (StringUtils.isNotBlank(infoDO.getImageItemJson())) {
                            introductionInfoVO.setImageTextItemVOS(JSONArray.parseArray(infoDO.getImageItemJson(), ImageTextItemVO.class));
                        }
                        return introductionInfoVO;
                    }).collect(Collectors.toList());
                    equipmentInfoVO.setIntroductionInfoVOS(introductionInfoVOS);
                }
            }
            return equipmentInfoVO;
        }).collect(Collectors.toList());
        Page<EquipmentInfoVO> carInfoVOPage = new Page<>();
        carInfoVOPage.setCurrent(page.getCurrent());
        carInfoVOPage.setTotal(page.getTotal());
        carInfoVOPage.setSize(page.getSize());
        carInfoVOPage.setPages(page.getPages());
        carInfoVOPage.setRecords(equipmentInfoVOS);
        return ServerResult.success(PageUtils.getUnifiedPageReturn(carInfoVOPage));
    }


    /**
     * @param queryVO
     * @return
     */
    private LambdaQueryWrapper<EquipmentInfoDO> buildQueryWrapper(EquipmentInfoQueryVO queryVO) {
        if (StringUtils.isNotBlank(queryVO.getDistrict())) {
            List<AddressInfoDO> addressInfoDOS = addressInfoService.list(new LambdaQueryWrapper<AddressInfoDO>().eq(AddressInfoDO::getEnableFlag, Constant.ENABLE_TEN).like(AddressInfoDO::getCountyName, queryVO.getDistrict()));
            if (CollectionUtils.isNotEmpty(addressInfoDOS)) {
                List<Long> addressIds = addressInfoDOS.stream().map(AddressInfoDO::getId).collect(Collectors.toList());

                List<AddressBindingDO> bindingDOS = addressBindingService.list(new LambdaQueryWrapper<AddressBindingDO>().in(AddressBindingDO::getAddressId, addressIds));
                if (CollectionUtils.isNotEmpty(bindingDOS)) {
                    queryVO.setIds(bindingDOS.stream().map(AddressBindingDO::getEquipmentId).collect(Collectors.toList()));
                } else {
                    queryVO.setIds(Arrays.asList(-1L));
                }
            } else {
                queryVO.setIds(Arrays.asList(-1L));
            }
        }
        LambdaQueryWrapper<EquipmentInfoDO> queryWrapper = new LambdaQueryWrapper<EquipmentInfoDO>()
                .eq(EquipmentInfoDO::getEnableFlag, Constant.ENABLE_TEN)
                .in(CollectionUtils.isNotEmpty(queryVO.getIds()), EquipmentInfoDO::getId, queryVO.getIds())
                .like(StringUtils.isNotBlank(queryVO.getName()), EquipmentInfoDO::getName, queryVO.getName())
                .like(StringUtils.isNotBlank(queryVO.getKeyword()), EquipmentInfoDO::getName, queryVO.getKeyword())
                .eq(StringUtils.isNotBlank(queryVO.getBrandCode()), EquipmentInfoDO::getBrandCode, queryVO.getBrandCode())
                .eq(StringUtils.isNotBlank(queryVO.getBrandName()), EquipmentInfoDO::getBrandName, queryVO.getBrandName())
                .eq(StringUtils.isNotBlank(queryVO.getTypeCode()), EquipmentInfoDO::getTypeCode, queryVO.getTypeCode())
                .eq(StringUtils.isNotBlank(queryVO.getTypeName()), EquipmentInfoDO::getTypeName, queryVO.getTypeName())
                .eq(StringUtils.isNotBlank(queryVO.getModelCode()), EquipmentInfoDO::getModelCode, queryVO.getModelCode());

        if ("1".equals(queryVO.getPrice())) {
            queryWrapper.lt(EquipmentInfoDO::getPrice, 10000);
        } else if ("2".equals(queryVO.getPrice())) {
            queryWrapper.gt(EquipmentInfoDO::getPrice, 10000);
            queryWrapper.lt(EquipmentInfoDO::getPrice, 50000);
        } else if ("3".equals(queryVO.getPrice())) {
            queryWrapper.gt(EquipmentInfoDO::getPrice, 50000);
            queryWrapper.lt(EquipmentInfoDO::getPrice, 200000);
        } else if ("4".equals(queryVO.getPrice())) {
            queryWrapper.gt(EquipmentInfoDO::getPrice, 200000);
        }

        if (StringUtils.isBlank(queryVO.getSort())) {
            queryWrapper.orderByDesc(EquipmentInfoDO::getId);
        } else if ("1".equals(queryVO.getSort())) {
            queryWrapper.orderByDesc(EquipmentInfoDO::getPrice);
        } else if ("2".equals(queryVO.getSort())) {
            queryWrapper.orderByAsc(EquipmentInfoDO::getPrice);
        }
        return queryWrapper;
    }

    /**
     * 新增数据
     *
     * @param equipmentInfoVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult add(EquipmentInfoVO equipmentInfoVO) {
        if (Objects.nonNull(equipmentInfoVO.getId())) {
            equipmentInfoVO.setId(null);
        }
        EquipmentParamInfoVO paramInfoVO = equipmentInfoVO.getParamInfoVO();

        if (Objects.nonNull(paramInfoVO.getId())) {
            paramInfoVO.setId(null);
        }


        EquipmentInfoDO equipmentInfoDO = BeanUtil.copyProperties(equipmentInfoVO, EquipmentInfoDO.class);

        if (CollectionUtils.isNotEmpty(equipmentInfoVO.getImageItemVOList())) {
            equipmentInfoDO.setImgItems(JSONArray.toJSONString(equipmentInfoVO.getImageItemVOList()));
        }
        if (CollectionUtils.isNotEmpty(equipmentInfoVO.getMasterItemVOList())) {
            equipmentInfoDO.setMasterImgItems(JSONArray.toJSONString(equipmentInfoVO.getMasterItemVOList()));
        }
        if (CollectionUtils.isNotEmpty(equipmentInfoVO.getParamItemVOList())) {
            equipmentInfoDO.setParamImgItems(JSONArray.toJSONString(equipmentInfoVO.getParamItemVOList()));
        }
        if (CollectionUtils.isNotEmpty(equipmentInfoVO.getScopeItemVOList())) {
            equipmentInfoDO.setScopeImgItems(JSONArray.toJSONString(equipmentInfoVO.getScopeItemVOList()));
        }
        if (CollectionUtils.isNotEmpty(equipmentInfoVO.getIntroduceItemVOList())) {
            equipmentInfoDO.setIntroduceImgItems(JSONArray.toJSONString(equipmentInfoVO.getIntroduceItemVOList()));
        }

        EquipmentParamInfoDO paramInfoDO = new EquipmentParamInfoDO();
        BeanUtil.copyProperties(paramInfoVO, paramInfoDO);


        transactionHelper.run(() -> {
            save(equipmentInfoDO);
            if (Objects.nonNull(paramInfoDO)) {
                paramInfoDO.setEquipmentId(equipmentInfoDO.getId());
                paramInfoService.save(paramInfoDO);
            }
            List<EquipmentIntroductionInfoVO> introductionInfoVOS = equipmentInfoVO.getIntroductionInfoVOS();
            if (CollectionUtils.isNotEmpty(introductionInfoVOS)) {
                List<EquipmentIntroductionInfoDO> introductionInfoDOS = introductionInfoVOS.stream().map(x -> {
                    EquipmentIntroductionInfoDO introductionInfoDO = new EquipmentIntroductionInfoDO();
                    BeanUtil.copyProperties(x, introductionInfoDO);
                    if (CollectionUtils.isNotEmpty(x.getImageTextItemVOS())) {
                        introductionInfoDO.setLineTextJson(JSONArray.toJSONString(x.getLineTextItemVOS()));
                    }
                    if (CollectionUtils.isNotEmpty(x.getImageTextItemVOS())) {
                        introductionInfoDO.setImageItemJson(JSONArray.toJSONString(x.getImageTextItemVOS()));
                    }
                    introductionInfoDO.setEquipmentId(equipmentInfoDO.getId());
                    return introductionInfoDO;
                }).collect(Collectors.toList());
                introductionInfoService.saveBatch(introductionInfoDOS);
            }
            if (CollectionUtils.isNotEmpty(equipmentInfoVO.getAddressIds())) {
                List<AddressBindingDO> bindingDOS = equipmentInfoVO.getAddressIds().stream().map(x -> {
                    AddressBindingDO addressBindingDO = new AddressBindingDO();
                    addressBindingDO.setEquipmentId(equipmentInfoDO.getId());
                    addressBindingDO.setAddressId(x);
                    addressBindingDO.setEnableFlag(Constant.ENABLE_TEN);
                    return addressBindingDO;
                }).collect(Collectors.toList());

                addressBindingService.saveBatch(bindingDOS);
            }
        });

        return ServerResult.success();
    }

    /**
     * 修改数据
     *
     * @param equipmentInfoVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult edit(EquipmentInfoVO equipmentInfoVO) {
        if (Objects.isNull(equipmentInfoVO.getId())) {
            throw new BusinessException("设备id不能为空！");
        }
        EquipmentParamInfoVO paramInfoVO = equipmentInfoVO.getParamInfoVO();

        EquipmentInfoDO equipmentInfoDO = getById(equipmentInfoVO.getId());
        if (Objects.isNull(equipmentInfoDO)) {
            throw new BusinessException("设备不存在！");
        }
        BeanUtils.copyProperties(equipmentInfoVO, equipmentInfoDO);
        if (CollectionUtils.isNotEmpty(equipmentInfoVO.getImageItemVOList())) {
            equipmentInfoDO.setImgItems(JSONArray.toJSONString(equipmentInfoVO.getImageItemVOList()));
        }
        if (CollectionUtils.isNotEmpty(equipmentInfoVO.getMasterItemVOList())) {
            equipmentInfoDO.setMasterImgItems(JSONArray.toJSONString(equipmentInfoVO.getMasterItemVOList()));
        }
        if (CollectionUtils.isNotEmpty(equipmentInfoVO.getParamItemVOList())) {
            equipmentInfoDO.setParamImgItems(JSONArray.toJSONString(equipmentInfoVO.getParamItemVOList()));
        }
        if (CollectionUtils.isNotEmpty(equipmentInfoVO.getScopeItemVOList())) {
            equipmentInfoDO.setScopeImgItems(JSONArray.toJSONString(equipmentInfoVO.getScopeItemVOList()));
        }
        if (CollectionUtils.isNotEmpty(equipmentInfoVO.getIntroduceItemVOList())) {
            equipmentInfoDO.setIntroduceImgItems(JSONArray.toJSONString(equipmentInfoVO.getIntroduceItemVOList()));
        }

        EquipmentParamInfoDO paramInfoDO = new EquipmentParamInfoDO();
        BeanUtil.copyProperties(paramInfoVO, paramInfoDO);

        transactionHelper.run(() -> {
            updateById(equipmentInfoDO);
            if (Objects.nonNull(paramInfoDO)) {
                paramInfoDO.setEquipmentId(equipmentInfoDO.getId());
                paramInfoService.updateById(paramInfoDO);
            }
            List<EquipmentIntroductionInfoVO> introductionInfoVOS = equipmentInfoVO.getIntroductionInfoVOS();
            if (CollectionUtils.isNotEmpty(introductionInfoVOS)) {
                List<EquipmentIntroductionInfoDO> introductionInfoDOS = introductionInfoVOS.stream().map(x -> {
                    EquipmentIntroductionInfoDO introductionInfoDO = new EquipmentIntroductionInfoDO();
                    BeanUtil.copyProperties(x, introductionInfoDO);
                    if (CollectionUtils.isNotEmpty(x.getImageTextItemVOS())) {
                        introductionInfoDO.setLineTextJson(JSONArray.toJSONString(x.getLineTextItemVOS()));
                    }
                    if (CollectionUtils.isNotEmpty(x.getImageTextItemVOS())) {
                        introductionInfoDO.setLineTextJson(JSONArray.toJSONString(x.getImageTextItemVOS()));
                    }
                    introductionInfoDO.setEquipmentId(equipmentInfoDO.getId());
                    return introductionInfoDO;
                }).collect(Collectors.toList());
                introductionInfoService.updateBatchById(introductionInfoDOS);
            }
            if (CollectionUtils.isNotEmpty(equipmentInfoVO.getAddressIds())) {
                addressBindingService.remove(new LambdaQueryWrapper<AddressBindingDO>().eq(AddressBindingDO::getEquipmentId, equipmentInfoDO.getId()));
                List<AddressBindingDO> bindingDOS = equipmentInfoVO.getAddressIds().stream().map(x -> {
                    AddressBindingDO addressBindingDO = new AddressBindingDO();
                    addressBindingDO.setEquipmentId(equipmentInfoDO.getId());
                    addressBindingDO.setAddressId(x);
                    addressBindingDO.setEnableFlag(Constant.ENABLE_TEN);
                    return addressBindingDO;
                }).collect(Collectors.toList());

                addressBindingService.saveBatch(bindingDOS);
            }
        });
        return ServerResult.success();
    }

    /**
     * 通过主键删除数据
     *
     * @param equipmentInfoQueryVO 筛选条件
     * @return 是否成功
     */
    @Override
    public ServerResult deleteById(EquipmentInfoQueryVO equipmentInfoQueryVO) {
        List<Long> ids = equipmentInfoQueryVO.getIds();
        if (CollectionUtils.isEmpty(ids)) {
            return ServerResult.fail("ids 参数不能为空");
        }
        List<EquipmentInfoDO> equipmentInfoDOS = listByIds(ids);
        if (CollectionUtils.isEmpty(equipmentInfoDOS)) {
            return ServerResult.success();
        }
        LambdaUpdateWrapper<EquipmentInfoDO> equipmentInfoDODOLambdaUpdateWrapper = new LambdaUpdateWrapper<EquipmentInfoDO>()
                .in(EquipmentInfoDO::getId, ids)
                .set(EquipmentInfoDO::getEnableFlag, Constant.UNABLE_TWENTY);
        LambdaUpdateWrapper<EquipmentParamInfoDO> equipmentParamInfoDOLambdaUpdateWrapper = new LambdaUpdateWrapper<EquipmentParamInfoDO>()
                .in(EquipmentParamInfoDO::getEquipmentId, ids)
                .set(EquipmentParamInfoDO::getEnableFlag, Constant.UNABLE_TWENTY);

        LambdaUpdateWrapper<EquipmentIntroductionInfoDO> equipmentIntroductionInfoDOLambdaUpdateWrapper = new LambdaUpdateWrapper<EquipmentIntroductionInfoDO>()
                .in(EquipmentIntroductionInfoDO::getEquipmentId, ids)
                .set(EquipmentIntroductionInfoDO::getEnableFlag, Constant.UNABLE_TWENTY);
        transactionHelper.run(() -> {
            update(equipmentInfoDODOLambdaUpdateWrapper);
            paramInfoService.update(equipmentParamInfoDOLambdaUpdateWrapper);
            introductionInfoService.update(equipmentIntroductionInfoDOLambdaUpdateWrapper);
            addressBindingService.remove(new LambdaQueryWrapper<AddressBindingDO>().in(AddressBindingDO::getEquipmentId, ids));
        });
        return ServerResult.success();
    }
}
